<!DOCTYPE html>
<html>
<title>Registration Page :: DIF233 - Unit 1 - Excercise 2</title>
<body>
	<?php
		function get_connection(){
			$file = fopen("config.txt","r+") or exit("Sorry not able to open the register file");
			$line = fgets($file);
			$value = explode(":", $line);
			$connection = mysqli_connect($value[0], $value[1], $value[2], $value[3]);
			
			if(mysqli_connect_errno($connection)){
				echo "unable to connect to test data base";
				exit("unable to connect to test data base");
			}else{
				return $connection;
			}
		}
		function execute_query($connection, $query, $error_msg){
			if(!mysqli_query($connection, $query)){
				echo $error_msg.mysqli_error($connection);
				exit($error_msg.mysqli_error($connection));
			}
		}
		function is_user_exist($connection, $username){
			$get_user_details = "select id from user_credentials where user_name = '".$username."'";
			$result = mysqli_query($connection, $get_user_details);
			$count = 0;
			while($row = mysqli_fetch_array($result)){
				$count++;
			}
			if($count == 0){
				return 0;
			}else{
				return 1;
			}
		}
		function save($connection, $first_name,$last_name, $user_id,$password,$mobile_number,$email_id){
			$save_credentials = "insert into user_credentials (user_name, password) values ('"
					.$user_id."', '"
							.$password."')";
			$save_user_details = "insert into user_profile (user_id, user_firstname, user_lastname, mobile_number, email_id) values ("
					."last_insert_id(), '".$first_name."',"
					."'".$last_name."',"
					."'".$mobile_number."',"
					."'".$email_id."')";
			echo $save_user_details;
			execute_query($connection, $save_credentials, "Unable to save user credentials");
			execute_query($connection, $save_user_details, "Unable to save user details");
		}
		if(!empty($_POST["submit"])){
			$connection = get_connection();
			if(is_user_exist($connection, $_POST["user_id"])){
				echo "<h3>User Already exists please try with different user name</h3>";
			}else{
				save($connection, $_POST["first_name"],$_POST["last_name"], $_POST["user_id"],$_POST["password"],$_POST["mobile_number"],$_POST["email_id"]);
				header("Location: index.php?msg=success");
				exit();
			}
			mysqli_close($connection);
		}	
	?>
<form method="post" name="register" action="register.php">
	<table border="0" width="100%">
		<tr>
			<td>
				First Name:
			</td>
			<td>
				<input type="text" name="first_name" value=<?php echo '"', $_POST["first_name"], '"' ?> required/>
			</td>
		</tr>
		<tr>
			<td>
				Last Name:
			</td>
			<td>
				<input type="text" name="last_name" value=<?php echo '"', $_POST["last_name"], '"' ?> required/>
			</td>
		</tr>
		<tr>
			<td>
				User Name:
			</td>
			<td>
				<input type="text" name="user_id" required/>
			</td>
		</tr>
		<tr>
			<td>
				Password:
			</td>
			<td>
				<input type="password" name="password" value=<?php echo '"', $_POST["password"], '"' ?> required/>
			</td>
		</tr>		
		<tr>
			<td>
				Mobile Number:
			</td>
			<td>
				<input type="text" name="mobile_number" value=<?php echo '"', $_POST["mobile_number"], '"' ?> required/>
			</td>
		</tr>		
		<tr>
			<td>
				Email Id:
			</td>
			<td>
				<input type="email" name="email_id" value=<?php echo '"', $_POST["email_id"], '"' ?> required/>
			</td>
		</tr>
		<tr>
			<td colspan="2"><input type="submit" name="submit" value="submit"/></td>
		</tr>		
	</table>
</form>
</body>
</html>
